import request from 'lib/request';

// 获取所有测评记录
// res: [{ Name, Time, Type, Code, Id }, ...]
export function queryScaleList (hospitalId, familyId, isFinish){
  let body = {
    hospitalId,
    familyId,
    isFinish,
  }
  let fId =JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).patientId;
  let hId = JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).hospitalId;
  let isf = 0;
  return request("wechat/eva/list?"+"hospitalId="+hId+"&familyId="+fId+"&isFinish="+isf,{
    method: "GET",
  })
}

// 获取测评记录二维码
// res: { id: number, code: string, image: string }
export function queryScaleQrcode(id, code) {
  const body = new FormData();
  body.append('id', id);
  body.append('code', code);
  return request.post('/scale/qrcode', { body });
}
export function queryScaleEva(type, uid){
   let body = {
    uid,
  }
  return request(`wechat/${type}/eva?`+"uid="+uid, {
    method: "GET",
  })
}

// 查看测评详情
// res: { Id: number, Type: string, ChoiceKey: string }
export function queryScaleDetail(type, uid, familyId){
  let body = {
    uid,
    familyId,
  }
  return request(`wechat/${type}/detail?`+"uid="+uid+"&familyId="+familyId, {
    method: "GET",
  })
}

// 获取测评题目
export function queryScaleQuestions({ type, gender }) {
  const body = new FormData();
  let Gender;
  // TODO
  if (sessionStorage.getItem('token')) {
    const locagender = location.href.substring(location.href.indexOf("gender"),location.href.indexOf("&",location.href.indexOf("gender")));
    if(locagender.substring(locagender.length-1,locagender.length) == 1){
      Gender = 1;
    }else{
      Gender = 2;
    }
  }
  if (type === 'nine') {
    if(gender){
      body.append('gender', gender);
    }else{
      body.append('gender', Gender);
    }
    
  }
  return request.post(`/scale/${type}`, { body });
}

// 删除测评纪录
export function deleteScaleRecord(id) {
  const body = new FormData();
  body.append('uid', id);
  return request.post('wechat/eva/delete', { body });
}

//微信和app的备孕和孕中用b字段,暂时用备用的0.
// 提交测评（微信端患者）
export function submitScaleByUser(type, patientId, patientValue, jw, sc) {
  const body = new FormData();
  body.hasPatientid = true;
  if(type === 'pre'){
      body.append('b', '0');
  }
  
  if(type === 'partum'){
     body.append('jw', jw);
     body.append('sc', sc);
  }
  body.append('patientId', patientId);
  
  body.append('familyId', JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).patientId);
  body.append('hospitalId', JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).hospitalId);
  body.append('patientValue', patientValue);

  return request.post(`wechat/${type}/submit`, { body });
}

export function submitScaleByUsers(type, patientId, patientValue, jw, sc, hy, height, weight, isReturn, isMiscarriage, isAbortion, doctorId) {
  const body = new FormData();
  body.hasPatientid = true;
  body.append('b', '0');
  body.append('doctorId', '0');
  if(type === 'prepre' || type === 'normal'){
      body.append('jw', jw);
      body.append('sc', sc);
      body.append('height', height);
      body.append('weight', weight);
      body.append('hy', hy);
      body.append('isReturn', isReturn);
      body.append('isMiscarriage', isMiscarriage);
      body.append('isAbortion', isAbortion);
  }
  body.append('patientId', patientId);
  body.append('familyId', JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).patientId);
  body.append('hospitalId', JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).hospitalId);
  body.append('patientValue', patientValue);

  return request.post(`wechat/${type}/create`, { body });
}
 
export function submitScaleByUser1(type, patientId, patientValue1, patientValue2, patientValue3){
  const body = new FormData();
  body.append('patientValue1', patientValue1);
  body.append('patientValue2', patientValue2);
  body.append('patientValue3', patientValue3);
  body.append('patientId', patientId);
  body.append('familyId', JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).patientId);
  body.append('hospitalId', JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).hospitalId);
  return request.post(`wechat/${type}/submit`, { body });
}

// 提交测评（APP端医生）
// data: doctorid, patientvalue
export function submitScaleByDoctor(type, data1, data2, doctorid, Age) {
  const body = new FormData();
  body.append('b', '0');
  // Object.keys(data).forEach(key => body.append(key, data[key]));
  body.append('patientid', data1);
  body.append('patientvalue', data2);
  body.append('doctorid', doctorid);
  return request.post(`/scale/${type}/step1`, { body });
}

// 提交调养方案（APP端医生）
// data: doctorid, patientvalue, report1, report2, report3, programerec, programeint, comment
export function submitScalePrograme(type, data) {
  const body = new FormData();
  // body.append("hid",hid);
  body.append('b', '0');
  Object.keys(data).forEach(key => body.append(key, data[key]));
  return request.post(`/scale/${type}/step2`, { body });
}

//获取healthy页面图片
export function pickImg(){
  return request(`wechat/banner/list`, {
    method: "GET",
  })
}
